#ifndef XMLER_H
#define XMLER_H

#include <QObject>
#include <QDomDocument>
#include <QFile>
#include <QSharedPointer>

class XMLer : public QObject
{
    Q_OBJECT
public:
    explicit XMLer(QObject *parent = nullptr);
    virtual ~XMLer();

    void open(const QString &filePath, bool create = false);
    void openFromString(const QString &content);
    void close();

    void writeString(QStringList path, QString value);
    void writeMap(QStringList path, QVariantMap value);
    QString readString(QStringList path);
    QVariantMap readMap(QStringList path);
    QVariantList readList(QStringList path);

private:
    QVariant getNodeValue(QDomNode &node);
    void saveNodeValue(QDomNode &node, QVariant value);
    void saveMapToNode(QDomNode &node, QString key, QString value);
    void saveStringToNode(QDomNode &node, QString value);
    void saveToFile();

    QDomNode findChild(QDomNode &node, QStringList path, bool create = false);
    QDomNode findChild(QDomNode &node, const QString &childName, int index,  bool create = false);
    QDomNode findChild(QDomNode &node, const QString &childName, bool create = false);

private:
    QSharedPointer<QFile> _file;
    QDomDocument _doc;
};

#endif // XMLER_H
